import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ProdSuppDialog extends javax.swing.JDialog {

private Statement stmt;	

Vector<String> columnNames = new Vector<String>();
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
private JTable jTable1;
private JScrollPane jScrollPane1;
public String PId;
private ResultSet thadsresult;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	* Auto-generated main method to display this JDialog
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JFrame frame = new JFrame();
				ProdSuppDialog inst = new ProdSuppDialog(frame);
				inst.setVisible(true);
			}
		});
	}
	
	public ProdSuppDialog(JFrame frame) 
	{
		super(frame);
		stmt =  Database_Functions.databaseConnection();
		initGUI();
	}
	
	public ProdSuppDialog(JPanel panel, String pId)
	{
		PId = pId;
		stmt =  Database_Functions.databaseConnection();
		//System.out.println(PId);
		initGUI();
		
	}
	private void initGUI() 
	{
		try 
		{
			
			this.setBounds(300, 300, 642, 359);
			getContentPane().setLayout(null);
			{
				jScrollPane1 = new JScrollPane();
				getContentPane().add(jScrollPane1);
				jScrollPane1.setBounds(61, 65, 516, 201);
				{
					thadsAgent();
					TableModel jTable1Model = 
						new DefaultTableModel(data,columnNames);
					jTable1 = new JTable();
					jScrollPane1.setViewportView(jTable1);
					jTable1.setModel(jTable1Model);
					jTable1.setBounds(61, 65, 516, 201);
					
					
				}
			}

		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
	
	protected void thadsAgent()
	{
		try
		{
			
			String sql ="select ps.productsupplierid,p.prodname,s.supname from products_suppliers ps join suppliers s on s.supplierid = ps.supplierid "+
				"join products p on ps.productid = p.productid "+
				"and productsupplierid in(select productsupplierid from packages_products_suppliers where packageid = "+PId+") "+
				"order by p.prodname,ps.productsupplierid";
			
			thadsresult = (ResultSet) stmt.executeQuery(sql);
			ResultSetMetaData md = thadsresult.getMetaData();
			int columns = md.getColumnCount();
			columnNames.addElement(md.getColumnName(1));
			columnNames.addElement(md.getColumnName(2));
			columnNames.addElement(md.getColumnName(3));
			while (thadsresult.next())
			{
					Vector<Object> row = new Vector<Object>(columns);
					row.addElement(thadsresult.getObject(1));
					row.addElement(thadsresult.getObject(2));
					row.addElement(thadsresult.getObject(3));
					data.addElement(row);
			}
			
			thadsresult.close();
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
	}

}
